package monsmon.game.tmlt;

import java.util.Map;
import java.lang.reflect.*;
import java.awt.Color;
import monsmon.game.Tile;
import monsmon.game.parse.LevelStrs;
import monsmon.game.parse.Params;
import monsmon.game.stile.*;

public class TileT{
	
	public static final String sp_prefix = "monsmon.game.stile.weffect.";

	public Color fg = Color.WHITE,
	       bg = Color.BLACK;
	public String name = "", fname = "";
	public char c = '?';
	public boolean walk = true;
	private String sp_name = null;
	private boolean has_sp = false;
	private Class<WalkEffect> sp_class = null;
	private Constructor<WalkEffect> sp_const = null;	

	public TileT()	{	}
	
	public TileT(Params p){
		name = p.gets("name", "ERROR");
		fname = p.gets("fname", name);
		fg = p.get_col("fg", Color.WHITE);
		bg = p.get_col("bg", Color.BLACK);
		c = p.getc("char", '?');
		walk = p.getb("walk", false);
		sp_name = p.gets("special", null);
		if(sp_name != null){
			try{
				sp_class = (Class<WalkEffect>) 
					Class.forName(sp_prefix + sp_name);
				sp_const = sp_class.getConstructor();
				has_sp = true;
			}catch(ClassNotFoundException|NoSuchMethodException
				       |SecurityException ex){
				ex.printStackTrace();
				System.err.printf("Could not find special" +
					" name: "+sp_name+" in %s\n", name);
			}
		}	
	}	

	public TileT(String name, char c, Color fg, Color bg, boolean walk){
		this.name = name;
		this.c = c;
		this.fg = fg;
		this.bg = bg;
		this.walk = walk;
	}

	public String get_name(){ return name;}
	public Color get_fg() { return fg;}
	public Color get_bg() { return bg;}
	public String get_fname() { return fname;}
	public char get_char() { return c;}
	public boolean can_walk() { return walk;}

	public String toString(){
		return "TileT: " + name + ", fg: " + fg + ", bg: " + bg + 
			", c: " + c + ", walk: " + walk;
	}

	public Tile create_tile(int x, int y){
		if(!has_sp){
			return new Tile(x, y, this);
		}else{
			WalkEffect ef = null;
			try{
				ef = sp_const.newInstance();
			}catch(InstantiationException|IllegalAccessException|
                    			IllegalArgumentException|
					InvocationTargetException ex){
				System.err.printf("Could not create special: "
					+ sp_name);
		     	}
			return new WalkOnTile(x, y, this, ef);
		}
	}

	private static TileT __defaults[] = {
		new TileT("floor", '.', Color.WHITE, Color.BLACK, true),
		new TileT("grass", '#', Color.GREEN, Color.BLACK, true),
		new TileT("water", '~', Color.BLUE, Color.BLACK, false),
		new TileT("wall", '%', Color.GRAY, Color.BLACK, false)
	};

	//Adds default values for map
	//	'.' -- ground
	//	'#' -- grass
	//	'~' -- water
	//	' ' -- none
	public static void add_defaults(Map<Character, TileT> aliases, 
				Map<String, TileT> tiles){
		for(TileT t : __defaults){
			tiles.put(t.name, t);
			aliases.put(t.c, t);
		}
	}
}

